#ifndef POSTGRESQL_H
#define POSTGRESQL_H

#include "postgresql_global.h"
#include "../idatabase.h"
#include <QtPlugin>


#include <QSqlQuery>

#include "postgresd.h"

#include <QtEndian>
#include <QSqlDriver>

class POSTGRESQLSHARED_EXPORT PostgreSQL : public IDatabase{
	Q_OBJECT
	Q_INTERFACES(IDatabase)
private:
	const int m_correctCountTables;
	const int m_defaultPort;
	bool m_SSL;
	QString m_prefix;

	PostgreSD *m_pSettingsInstance;

	inline void catchError(QString desc, const QSqlQuery &query);

	bool initiateDatabase( bool createDB = false );
	bool repairDatabase();
	void insertItem(const item &newItem);
	void recreate();
public:
	explicit PostgreSQL( QObject *parent = 0 );
	~PostgreSQL();

	bool connectToDB();
	bool disconnectFromDb();
	bool canConnect() const;

	void setSSL(bool ssl);
	bool useSSL() const;

	int getDefaultPort() const;

	quint64 checkLogin(const QString &login, const QString &password);
	bool addLogin(const QString &login, const QString &password);
	bool changePassword(const quint64 &id, const QString &oldPassword,
						const QString &newPassword);
	QList<ChannelEntry> getChannels(const quint64 &id);
	QList<NewsEntry> getNews(const quint64 &id,
							 const quint64 &channel,
							 const quint32 &lastSync);
	bool addChannel(const quint64 &id,
					const ChannelEntry &entry);
	bool subscribeChannel(const quint64 &id, const quint64 &channel);
	bool updateNews(const quint64 &id,
					const quint32 &news,
					const quint32 &timestamp,
					bool readed, bool deleted);

private slots:
	void postInit();

};

#endif // POSTGRESQL_H
